package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.dao.ProductDao;
import io.renren.modules.sys.entity.ProductEntity;
import io.renren.modules.sys.service.ProductService;
import io.renren.modules.sys.vo.ProductSelectVO;
import io.renren.modules.sys.vo.ProductVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Override
    public PageUtils queryPage( Map<String, Object> params) {
        Page<ProductVO> page = new Query<ProductVO>(params).getPage();
        page.setRecords(baseMapper.selectList(page,params));
        return new PageUtils(page);
    }


    @Override
    public List<ProductSelectVO> getProducs(String nameOrCode) {
        List<ProductVO>list=baseMapper.getProducts(nameOrCode);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        List<ProductSelectVO>productSelectVOS = new ArrayList<>();
        for (ProductVO productVO : list){
            ProductSelectVO productSelectVO = new ProductSelectVO();
            productSelectVO.setId(productVO.getId());
            productSelectVO.setName(productVO.getSupplierName()+"-"+productVO.getName()+ "-" + productVO.getCode() +"-"+productVO.getMaterial()+"-"+productVO.getModel());
            productSelectVOS.add(productSelectVO);
        }
        return productSelectVOS;
    }

    @Override
    public ProductVO selectById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void updateNumber(Long proId, Integer number) {
        baseMapper.updateNumber(proId,number);
    }

}
